package com.xin.net 
{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;

/**
 * ...
 * @author xin
 */
public class MP3Loader extends LoaderBase 
{
	private var _sound:Sound;
	
	public function MP3Loader(url:String) 
	{
		super(url);
	}
	
	override protected function onIoError(e : IOErrorEvent) : void
	{
		super.onIoError(e);
		
		removeListeners();
		_sound.close();
	}
	
	override protected function onSecurityError(e : SecurityErrorEvent) : void
	{
		super.onSecurityError(e);
		
		removeListeners();
		dispatchEvent(new Event(Event.COMPLETE));
	}
	
	override protected function removeListeners():void
	{
		_sound.removeEventListener(ProgressEvent.PROGRESS, onProgress);
		_sound.removeEventListener(Event.COMPLETE, _onComplete);
		_sound.removeEventListener(IOErrorEvent.IO_ERROR, onIoError);
		_sound.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
	}
	
	override protected function _onComplete(e : Event) : void
	{
		removeListeners();
		_sound.close();
		
		dispatchEvent(new Event(Event.COMPLETE));
	}
	
	override public function load():void
	{
		_sound = new Sound();
		_sound.addEventListener(ProgressEvent.PROGRESS, onProgress);
		_sound.addEventListener(Event.COMPLETE, _onComplete);
		_sound.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
		_sound.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		
		var urlRequest:URLRequest = new URLRequest(_url);
		urlRequest.method = URLRequestMethod.GET;
		_sound.load(urlRequest);
	}
	
	public function getSound():Sound
	{
		return _sound;
	}
	
}

}